<?php

namespace Translater;

use GuzzleHttp\Client;

class Google
{
    private $url;

    public function __construct($url)
    {
        $this->url = $url;
    }

    //翻译支持数组与字符串
    public function get($v, $from = 'en', $to)
    {
        $data = array(
            'sl' => $from,
            'tl' => [$to],
            'text' => !empty($v) && is_array($v) ? $v : [$v]
        );
        $r = curl_post_json($this->url, $data);
        $data = json_decode($r, true);
        if (isset($data['errorcode']) && $data['errorcode'] == 1 && isset($data['data'][$to][0])) {
            return !empty($v) && is_array($v) ? $data['data'][$to] : $data['data'][$to][0];
        } else {
            return !empty($v) && is_array($v) ? $v : 'translation error call QQ:279197963';
        }
    }
}